<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
	
	public	function __construct()
	{
		parent::__construct();
		if($this->session->userdata('user_id')){
			$this->load->helper("html");
			$this->load->model("tintucs");
			$this->load->model("tailieus");
			$this->load->model("giaobans");
			$this->load->model("users");			
			$this->load->model("lich");
			$this->load->model("log_model");
			date_default_timezone_set('Asia/Ho_Chi_Minh');
		}
		else{
			redirect('login');
		}
	}
	
	public function index($msg = null, $parameters = null)
	{
		$data['menu'] = 'user_'.$msg;
		$data['list_tailieu'] = $this->tailieus->listTailieu();
		$data['list_giaoban'] = $this->giaobans->listGiaoban();
		/////////////
		$data['menu_tin'] = $this->giaobans->listGiaoban();
		$list = array();
		foreach ($data['menu_tin'] as $l){		//sap xep nhom danh sach theo giao ban cha
			if($l->id_type_giaoban != 9)
				$list[$l->cha_giaoban][] = $l;
		}
		
		$data['multi_menu_tin'] = $this->giaobans->multiGiaoban($list,0);						
		////////////
		$data['list_lich'] = $this->lich->listLich();
		$this->load->view('header_block_v1',$data);
		$this->load->view('content_sidebar_block',$data);
		switch ($msg){
			case null:
				$this->viewAllUser();
				break;
			case 'viewall':
				$this->viewAllUser();
				break;
			case 'danhbas':
				$this->viewDanhba();
				break;
			case 'viewadd':
				$this->viewAddUser();
				break;
			case 'viewedit':
				$this->viewEditUser($parameters);
				break;
			case 'changepass':
				$this->viewChangePass();
				break;
			default:
				$this->viewAllUser();
				break;			
		}
		$this->load->view('footer_block');
	}
	public function viewChangePass()
	{
		$id_nguoidung = $this->session->userdata("user_id");
		$data['nguoidung'] = $this->users->getU($id_nguoidung);

		$this->load->view("users/viewChangePass",$data);
	}
	public function viewDanhba()
	{
		$data['alluser'] = $this->users->showUser();
	
		$this->load->view("users/viewDanhba",$data);
	}
	
	public function viewAllUser()
	{
		$data['alluser'] = $this->users->showUser();
		$listlich = $this->users->listLich();
		foreach($listlich as $list){
			$data['listLich'][$list['id_danhsachlich']] = $list['ten_lich'];
		}
		$this->load->view("users/viewAllUser",$data);
	}
	public function viewEditUser($id_nguoidung)
	{
		$data['nguoidung'] = $this->users->getU($id_nguoidung);
		$data['listLich'] = $this->users->listLich();
		$data['listQuyen'] = $this->users->listQuyen();
		$quyen = $this->users->getQuyen($id_nguoidung);
		if(count($quyen) > 0){
			foreach($quyen as $q){
				$data['quyen'][] = $q->id_quyen;
			}
		}
		else
			$data['quyen'] = array();
		$this->load->view("users/viewEditUser",$data);
	}
	
	public function viewAddUser()
	{
		$this->load->library('form_validation');
		$data['listLich'] = $this->users->listLich();
		$data['listQuyen'] = $this->users->listQuyen();

		$this->load->view("users/viewAddUser",$data);
	}
	//function
	public function changePass()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form		
		$this->form_validation->set_rules('matkhau_nguoidung', 'Mật khẩu cũ', 'xss_clean|required|min_length[5]|callback_password_check|md5');
		$this->form_validation->set_rules('matkhau_nguoidung_new', 'Mật khẩu mới', 'xss_clean|required|min_length[5]|md5');
		if ($this->form_validation->run() == FALSE){
			$this->index('changepass');
		}
		else {
			$user_id = $this->session->userdata('user_id');
			$data["matkhau_nguoidung"] = $this->input->post("matkhau_nguoidung_new");

			//print_r($quyen);
			$url = base_url();
			//print_r($this->users->insertUser($data,$quyen));
			if($this->users->changePass($data,$user_id)){
				if($this->log_model->saveLog(4,null,null,"Mật khẩu"))
					echo "<script>alert('Mật khẩu đã được đổi');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function password_check($password){
		$user_id = $this->session->userdata('user_id');
		$result = $this->users->password_check($password,$user_id);
		 
		if (!$result)
		{
			$this->form_validation->set_message('password_check', '%s không đúng');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	
	public function addUser()
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('ten_nguoidung', 'Tên đăng nhập', 'trim|xss_clean|required|min_length[4]|callback_username_check');
		$this->form_validation->set_rules('matkhau_nguoidung', 'Mật khẩu', 'trim|xss_clean|required|min_length[5]|md5');	
		$this->form_validation->set_rules('sodienthoai_nguoidung', 'Số điện thoại', 'trim|xss_clean|numeric');
		$this->form_validation->set_rules('email_nguoidung', 'Email', 'trim|xss_clean|valid_email');
		$this->form_validation->set_rules('tenthat_nguoidung', 'Tên thật', 'trim|xss_clean');
		$this->form_validation->set_rules('chucvu_nguoidung', 'Chức vụ', 'trim|xss_clean');
		$this->form_validation->set_rules('ngaysinh_nguoidung', 'Ngày sinh', 'trim|xss_clean');
		$this->form_validation->set_rules('phong_nguoidung', 'Phòng ban', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE){
			$this->index('viewadd');
		}
		else {
			$data["ten_nguoidung"] = $this->input->post("ten_nguoidung");
			$data["matkhau_nguoidung"] = $this->input->post("matkhau_nguoidung");
			$data["tenthat_nguoidung"] = $this->input->post("tenthat_nguoidung");
			$data["chucvu_nguoidung"] = $this->input->post("chucvu_nguoidung");
			$data["ngaysinh_nguoidung"] = $this->input->post("ngaysinh_nguoidung");
			$data["phongban_nguoidung"] = $this->input->post("phongban");
			$data["phong_nguoidung"] = $this->input->post("phong_nguoidung");
			$data["sodienthoai_nguoidung"] = $this->input->post("sodienthoai_nguoidung");
			$data["email_nguoidung"] = $this->input->post("email_nguoidung");
						
			if($this->input->post("quyen"))					
				$quyen = $this->input->post("quyen");
			else 
				$quyen = array();
			//print_r($quyen);
			$url = base_url()."user";
			//print_r($this->users->insertUser($data,$quyen));
			if($this->users->insertUser($data,$quyen)){	
				if($this->log_model->saveLog(3,null,null,"Người dùng mới ".$data["ten_nguoidung"]))
					echo "<script>alert('Bạn đã thêm 1 người dùng mới');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function username_check($username){		
			$result = $this->users->username_check($username);
			if ($result)
			{
				$this->form_validation->set_message('username_check', '%s đã tồn tại');
				return FALSE;
			}
			else
			{
				return TRUE;
			}
		}
	public function editUser($id_nguoidung)
	{		
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		//$this->form_validation->set_rules('ten_nguoidung', 'Tên đăng nhập', 'xss_clean|required|min_length[5]');
		
		$this->form_validation->set_rules('matkhau_nguoidung', 'Mật khẩu', 'trim|xss_clean|min_length[5]|md5');
		$this->form_validation->set_rules('sodienthoai_nguoidung', 'Số điện thoại', 'trim|xss_clean|is_natural|max_length[15]');
		$this->form_validation->set_rules('email_nguoidung', 'Email', 'trim|valid_email');
		$this->form_validation->set_rules('tenthat_nguoidung', 'Tên thật', 'trim|xss_clean');
		$this->form_validation->set_rules('chucvu_nguoidung', 'Chức vụ', 'trim|xss_clean');
		$this->form_validation->set_rules('ngaysinh_nguoidung', 'Ngày sinh', 'trim|xss_clean');
		$this->form_validation->set_rules('phong_nguoidung', 'Phòng ban', 'trim|xss_clean');
		if ($this->form_validation->run() == FALSE){
			//echo "Lỗi";
			$this->index('viewedit',$id_nguoidung);
		}
		else {	
			//print_r($this->input->post());
		if($this->input->post("matkhau_nguoidung") != "")	
			$data["matkhau_nguoidung"] = $this->input->post("matkhau_nguoidung");
		$data["tenthat_nguoidung"] = $this->input->post("tenthat_nguoidung");
		$data["chucvu_nguoidung"] = $this->input->post("chucvu_nguoidung");
		$data["ngaysinh_nguoidung"] = $this->input->post("ngaysinh_nguoidung");
		$data["phongban_nguoidung"] = $this->input->post("phongban");
		$data["phong_nguoidung"] = $this->input->post("phong_nguoidung");
		$data["sodienthoai_nguoidung"] = $this->input->post("sodienthoai_nguoidung");
		$data["email_nguoidung"] = $this->input->post("email_nguoidung");
		//print_r($data);
		if($this->input->post("quyen"))					
				$quyen = $this->input->post("quyen");
			else 
				$quyen = array();
		//print_r($quyen);
		$url = base_url()."user/danhbas";
		if($this->users->updateUser($data,$id_nguoidung,$quyen)){
			if($this->log_model->saveLog(4,null,null,"Người dùng ".$this->users->username($id_nguoidung)))
				echo "<script>alert('Cập nhật thành công');	window.location='".$url."'; </script>";
		}
		else			
			echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delUser($id_nguoidung)
	{
		$username = $this->users->username($id_nguoidung);
		if($this->users->delUser($id_nguoidung)){
			if($this->log_model->saveLog(1,null,null,"Người dùng ".$username))
			$url = base_url()."user";
			echo "<script>alert('Xóa thành công');	window.location='".$url."'; </script>";
		}
		else{
			echo "Lỗi: Không delete được!";
		}
			
	}
	
}